package util;

import java.util.Vector;


public class DjStringEditor {
	
	public static String deleteChar(char ch, String src, boolean allDel, boolean ignoreCase) {
		if (src.indexOf(ch) == -1) 
			return src;
		Vector charIndexVec = null;
		if (allDel) {
			charIndexVec = getCharIndexVec(ch, src, ignoreCase);
		} else {
			charIndexVec = new Vector(1);
			charIndexVec.addElement(new Integer(src.indexOf(ch)));
		}
		return deleteCharWithCharIndexVec(src, allDel, charIndexVec);
	}
	
	private static String deleteCharWithCharIndexVec(String src, boolean allDelete, Vector charIndexVec) {
		char[] srcChars = src.toCharArray();
		char[] dstChars = new char[srcChars.length];
		int dstCharsIndex = 0;
		
		int srcCharsLeng = srcChars.length;
		for (int i = 0; i < srcCharsLeng; i++) {
			if (allDelete) {
				if ((charIndexVec.indexOf(new Integer(i)) != -1)) 
					continue;
			} else {
				if (charIndexVec.elementAt(0).equals(new Integer(i)))
					continue;
			}
			dstChars[dstCharsIndex] = srcChars[i];
			dstCharsIndex ++;
		}
		return dstChars.toString().trim();
	}
	
	public static Vector getCharIndexVec( char ch, String src, boolean ignoreCase) {
		Vector charIndexVec = new Vector(0);
		for (int i = 0; i < src.length(); i++) {
			if (src.regionMatches(ignoreCase, i, String.valueOf(ch), 0, 1))
				charIndexVec.addElement(new Integer(i));
		}
		charIndexVec.trimToSize();
		return charIndexVec;
	}
	
	//가장 마지막에 있는 단어를 삭제함
	public static String delLastWord(final String source) {
		final String trimSource = source.trim();
		return source.substring(0, getInitialIndex(trimSource));
	}
	
	private static int getInitialIndex(final String trimSource) {
		return trimSource.lastIndexOf('\u0020') + 1;
	}
	
}
